package com.itnetwork.UtilityTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.junit.Test;

import com.itnetwork.commons.XmlToHtml;



public class XMLTool {
	
	@Test//ok
	public void DeleteXMLElement()//params:path,name
	{
		String path="E:\\news.xml";
		String name="nContent";
		XMLOutputter xmlOutputter = new XMLOutputter();
		File xmlFile=new File(path);
		//SAXBuilder sb = new SAXBuilder();
	
			Document document =this.LoadDocument(path);//sb.build(xmlFile);
			Element root=document.getRootElement();
			List list=root.getChildren();
			root.removeChild(name);
			document.setRootElement(root);
			try {
				xmlOutputter.output(document, new FileOutputStream(xmlFile));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
				System.out.println("success");
		

	}
	
	public Document LoadDocument(String path)
	{
		SAXBuilder sb = new SAXBuilder();
		Document document=null;
		try {
			document= sb.build(new File(path));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return document;
	}
	@Test
	public void UpdateXMLElement()//params:String path,String name,String text
	{
		String path="E:\\new.xml";
		String name="nContent";
		String text="callocallocallocallocallocallocallocallocallocallocallocallocallo<br>";
		Document document=this.LoadDocument(path);
		File file=new File(path);
		Element root=document.getRootElement();
		root.getChild(name).setText(text);
		XMLOutputter outter=new XMLOutputter();
		try {
			outter.output(document, new FileOutputStream(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//上面update成功 下面是update   html页面
		File filexsl=new File("E:\\new.xsl");
		StreamSource xslSource=new StreamSource(filexsl);
		StreamSource xmlSource= new StreamSource(file);
		File htmlFile=new File("E:\\new.html");
		try {
			XmlToHtml.write(xslSource, xmlSource, htmlFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		System.out.println("Ok");
	}
	
	public void getElementText(){//params:String path,String name
		
		String path="E:\\new.xml";
		String name="nContent";
	}
	/*public static void  main(String[] args){
		
	}*/
}
